package com.abocode.aichat.app.configuration;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by guanxf on 2015/12/16.
 */
//@Configuration
public class RedisConfiguration {
    /***
     ###jedis##pool##config###
     #url
     redis.server.url=172.16.4.18
     #port
     redis.server.port=6379
     #maxIdle
     redis.pool.maxIdle=200
     #maxWaitMillis
     redis.pool.maxWaitMillis=1000
     #testOnBorrow
     redis.pool.testOnBorrow=true
     #testOnReturn
     redis.pool.testOnReturn=false
     */
    @Value("${redis.server.url}")
    private String redisUrl;

    @Value("${redis.server.port}")
    private int port;

    @Value("${redis.pool.maxIdle}")
    private int  maxIdle;

    @Value("${redis.pool.maxWaitMillis}")
    private int maxWaitMillis;

    @Value("${redis.pool.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${redis.pool.testOnReturn}")
    private boolean testOnReturn;
    @Bean
    public JedisPoolConfig jedisPoolConfig(){
        JedisPoolConfig jedisPoolConfig= new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        jedisPoolConfig.setTestOnBorrow(testOnBorrow);
        jedisPoolConfig.setTestOnReturn(testOnReturn);
        return  jedisPoolConfig;
    }
    @Bean
    public Jedis jedis(){
        JedisPool jedisPool = new JedisPool(jedisPoolConfig(),redisUrl,port);
        return   jedisPool.getResource();
    }
}
